<html>
<head>
<title> JSONPath - Tests (js)</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="jsonpath.js"></script>
</head>
<body>
<pre>
<script type="text/javascript">
var out = "", tests = 
[ { "o": { a: "a",
           b: "b",
           "c d": "e" 
         },
    "p": [ "$.a",
           "$['a']",
           "$.'c d'",
           "$.*",
           "$['*']" ,
           "$[*]"
         ]
  },
  { "o": [ 1, "2", 3.14, true, null ],
    "p": [ "$[0]",
           "$[4]",
           "$[*]",
	   "$[-1:]"
         ]
  },
  { "o": { points: [
             { id: "i1", x:  4, y: -5 },
             { id: "i2", x: -2, y:  2, z: 1 },
             { id: "i3", x:  8, y:  3 },
             { id: "i4", x: -6, y: -1 },
             { id: "i5", x:  0, y:  2, z: 1 },
             { id: "i6", x:  1, y:  4 }
           ]
         },
    "p": [ "$.points[1]",
           "$.points[4].x",
           "$.points[?(@.id=='i4')].x",
           "$.points[*].x",
           "$['points'][?(@.x*@.x+@.y*@.y > 50)].id",
           "$.points[?(@.z)].id",
           "$.points[(@.length-1)].id"
         ]
  },
  { "o": { "menu": {
             "header": "SVG Viewer",
             "items": [
                 {"id": "Open"},
                 {"id": "OpenNew", "label": "Open New"},
                 null,
                 {"id": "ZoomIn", "label": "Zoom In"},
                 {"id": "ZoomOut", "label": "Zoom Out"},
                 {"id": "OriginalView", "label": "Original View"},
                 null,
                 {"id": "Quality"},
                 {"id": "Pause"},
                 {"id": "Mute"},
                 null,
                 {"id": "Find", "label": "Find..."},
                 {"id": "FindAgain", "label": "Find Again"},
                 {"id": "Copy"},
                 {"id": "CopyAgain", "label": "Copy Again"},
                 {"id": "CopySVG", "label": "Copy SVG"},
                 {"id": "ViewSVG", "label": "View SVG"},
                 {"id": "ViewSource", "label": "View Source"},
                 {"id": "SaveAs", "label": "Save As"},
                 null,
                 {"id": "Help"},
                 {"id": "About", "label": "About Adobe CVG Viewer..."}
             ]
           }
         },
    "p": [ "$.menu.items[?(@ && @.id && !@.label)].id",
           "$.menu.items[?(@ && @.label && /SVG/.test(@.label))].id",
           "$.menu.items[?(!@)]",
		   "$..[0]"
         ]
  },
  { "o": { a: [1,2,3,4],
           b: [5,6,7,8]
         },
    "p": [ "$..[0]",
	       "$..[-1:]",
		   "$..[?(@%2==0)]"
         ]
  },
  { "o": { lin: {color:"red", x:2, y:3},
           cir: {color:"blue", x:5, y:2, r:1 },
           arc: {color:"green", x:2, y:4, r:2, phi0:30, dphi:120 },
           pnt: {x:0, y:7 }
         },
    "p": [ "$.'?(@.color)'.x",
           "$['lin','cir'].color"
         ]
  },
  { "o": { lin: {color:"red", x:2, y:3},
           cir: {color:"blue", x:5, y:2, r:1 },
           arc: {color:"green", x:2, y:4, r:2, phi0:30, dphi:120 },
           pnt: {x:0, y:7 }
         },
    "p": [ "$.'?(@.color)'.x",
           "$['lin','arc'].color"
         ]
  },
  { "o": { text: [ "hello", "world2.0"] },
    "p": [ "$.text[?(@.length > 5)]",
           "$.text[?(@.charAt(0) == 'h')]"
         ]
  },
  { "o": { a: { a:2, b:3 },
           b: { a:4, b:5 },
           c: { a: { a:6, b:7}, c:8}
         },
    "p": [ "$..a"
         ]
  },
  { "o": { a: [ { a:5, '@':2, '$':3 },   // issue 7: resolved by escaping the '@' character 
                { a:6, '@':3, '$':4 },   // in a JSONPath expression.
                { a:7, '@':4, '$':5 } 
              ]
         },
    "p": [ "$.a[?(@['\\@']==3)]",
           "$.a[?(@['$']==5)]"
         ]
  }
];

function evaluate($, p) { var res = eval(p); return res != null ? res.toJSONString() : null; }

for (var i=0; i<tests.length; i++) {
   var pathes;
   for (var j=0; j<tests[i].p.length; j++) {
      pre = ">";
      if (pathes = jsonPath(tests[i].o, tests[i].p[j], {resultType: "PATH"}))
         for (var k=0; k<pathes.length; k++) {
            out += pre + " " + pathes[k] + " = " + evaluate(tests[i].o, pathes[k]) + "\n";
            pre = " ";
         }
      }
	  out += "<hr/>";
   }
   document.write(out);
  </script>
</pre>
</body>
</html>
